<?php

function SpaService_service_insert(){
  $ctrl = FormUtil::getPassedValue('ctrl',FALSE,'REQUEST');
  $key = FormUtil::getPassedValue('key',FALSE,'REQUEST');
  $config = pnModGetVar('SpaService', 'config');

  if($key!=$config.secretKey){
    return LogUtil::registerError ("You don't have permission to access this page.");
  }
  if (empty($ctrl)){
    if ($form[ctrl]){
      $ctrl = $form[ctrl];      
    }else{
      return 'ERROR SpaService system can not find controller variable';
    }
  }
  
  if (!($class = Loader::loadClassFromModule ('SpaService', $ctrl, false))){
    return LogUtil::registerError ("Unable to load class [$ctrl] ...");
  }
  $object  = new $class ();
  $object->getDataFromInputJSON();
  
  if($object->save()){
    $result = $object->getData();
    echo json_encode($result);
    exit;
  }else{
    $result['ErrorsMessage'] = 'Error! Someting wrong.';
    $result['ErrorCode'] = '01';
    echo json_encode($result);
    exit;
  }
}

function SpaService_service_select(){
  $ctrl = FormUtil::getPassedValue('ctrl',FALSE,'REQUEST');
  $id = FormUtil::getPassedValue('id',FALSE,'REQUEST');
  $key = FormUtil::getPassedValue('key',FALSE,'REQUEST');
  $config = pnModGetVar('SpaService', 'config');
  
  if(!$key){
    return LogUtil::registerError ("You don't have permission to access this page.");
  }
  if($key!=$config['secretKey']){
    return LogUtil::registerError ("You don't have permission to access this page.");
  }
  
  if ($id){
    //load class
    if (!($class = Loader::loadClassFromModule ('SpaService',$ctrl, false)))
      return LogUtil::registerError ("Unable to load class [$ctrl] ...");
  
    $object  = new $class ();
    $object->get($id);
    
    echo json_encode($object->_objData);
    exit;
  }else{
    //load class
    if (!($class = Loader::loadClassFromModule ('SpaService',$ctrl.'Array', false)))
      return LogUtil::registerError ("Unable to load class [$ctrl.'Array'] ...");
  
    $object  = new $class ();
    $object->get();
    
    echo json_encode($object->_objData);
    exit;
  }
}

function SpaService_service_delete(){

  $ctrl = FormUtil::getPassedValue('ctrl',FALSE,'REQUEST');
  $id = FormUtil::getPassedValue('id',FALSE,'REQUEST');
  $key = FormUtil::getPassedValue('key',FALSE,'REQUEST');
  $config = pnModGetVar('SpaService', 'config');
  
  if(!$key){
    return LogUtil::registerError ("You don't have permission to access this page.");
  }
  if($key!=$config['secretKey']){
    return LogUtil::registerError ("You don't have permission to access this page.");
  }
  
  if ($id){
    //load class
    if (!($class = Loader::loadClassFromModule ('SpaService',$ctrl, false)))
      return LogUtil::registerError ("Unable to load class [$ctrl] ...");
  
    $object = new $class ();
    $object->setDataField('id',$id);
    $tmp = $object->delete($id);
     if($tmp){
        $result['Message'] = 'Successful.';        
        echo json_encode($result);       
     }else{
        $result['ErrorMessage'] = 'Error! Not Successful.';
        $result['ErrorCode'] = '02';
        echo json_encode($result);         
     }  
    
    echo 'Successful';
    exit;
  }else{
    $result['ErrorsMessage'] = 'Error! ID not found.';
    $result['ErrorCode'] = '03';
    echo json_encode($result);
    exit;
  }
}

?>